define(["ajaxBase", "screenAdopt", "tool", "layer"], function (ajaxBase, screenAdopt, tool, layer) {
    let mainToPage = function () {
        this._data = null
        this._url = null
        this._method = null
        this._type = "JSON"
        this._shareArray = []
        this._adjustWindowMethod = null
    }
    mainToPage.setBase = function (data, url, method) {
        this._data = data
        this._url = url
        this._method = method
    }
    //设置总节点信息
    mainToPage.setNodeListToShow = function (nodeListToShow, adjustWindowMethod) {
        this._shareArray = nodeListToShow
        this._adjustWindowMethod = adjustWindowMethod
    }
    //设置第一次请求
    mainToPage.setFirstRequest = function (flag) {
        let that = this
        ajaxBase(this._data, this._url, this._method).then(function (data) {
            if (data && (!data.shareList || data.shareList.length === 0)) {
                return
            }
            let length = that._adjustWindowMethod(that._shareArray.getPlayNode())
            that._shareArray.showAllNode(data.shareList, length, that._data.pageNum, flag)
        })
    }

    mainToPage.setWindowResize = function (nodeName) {
        let that = this
        tool.on(window, 'resize', function () {
            clearTimeout(tool.timer)
            tool.timer = setTimeout(function () {
                let arrayNodeList = screenAdopt.getAllNextChildNode(nodeName)
                screenAdopt.removeChildrenNodes(nodeName, arrayNodeList.arrayAllChildrenNode)
                let length = that._adjustWindowMethod(that._shareArray.getPlayNode())
                that._shareArray.adjustScreen(arrayNodeList, length)
            }, 50)
        })
    }
    mainToPage.setRollWithWindow = function (nodeName1, nodeName2, lengthOfBottom, flag) {//nodeName1为父节点（一般是含有inner的），nodeName2为包含显示块的节点
        let scroll = document.querySelector("." + nodeName1),
            lengthToAscend = 0,
            that = this
        if (lengthOfBottom) {
            lengthToAscend = lengthOfBottom
        }
        tool.on(scroll, 'scroll', function () {
            clearTimeout(tool.timer2)
            if (screenAdopt.getMinHeight(nodeName2) + lengthToAscend < tool.getScrollTop(scroll)) {
                tool.timer2 = setTimeout(function () { //缓存
                    that._data.pageNum++   //分页
                    let layerModel = layer.msg('正在加载下一页!', {time: 2000})
                    ajaxBase(that._data, that._url, that._method).then(function (data) {
                        if (data && (!data.shareList || data.shareList.length === 0)) {
                            layer.close(layerModel)
                            layer.msg('已拉到最底部!', {time: 3000})
                            return
                        }
                        if (data.shareList && data.shareList.length >= 1) {
                            layer.close(layerModel)
                            let length = that._adjustWindowMethod(that._shareArray.getPlayNode())
                            that._shareArray.showAllNode(data.shareList, length, that._data.pageNum, flag)
                        }
                    })
                }, 50)
            }
        })
    }
    return mainToPage
})